<?php
	require_once 'lib/view.php';

/**
 * Prints human readable info about $param
 *
 * @param mixed $param info to display
 * @param bool $isDie
 */
function _d($param, $isDie=false)
{
    echo '<hr><pre><xmp>';
    if (is_bool($param))
    	$param = $param? "True" : "False";
    print_r($param);
    echo '</xmp></pre><hr>';
    if ($isDie)
        die();
}


/** Sets custom error handler
*/
function _ehandler($errNo, $errMsg, $fileName, $lineNum, $vars)
{
	$errLevel = _cfg("error.level");

    if ($errNo == 8)
        return;

    $types = array (
        1   =>  "Error", //E_ERROR
        2   =>  "Warning", //E_WARNING
        4   =>  "Parse", //E_PARSE 
        8   =>  "Notice",// E_NOTICE
        16  =>  "Core Error",//E_CORE_ERROR
        32  =>  "Core Warning", //E_CORE_WARNING
        64  =>  "Compile Error", //E_COMPILE_ERROR 
        128 =>  "Compile Warning", //E_COMPILE_WARNING        
        256 =>  "User Error", //E_USER_ERROR
        512 =>  "User Warning",//E_USER_WARNING
        1024=>  "User Notice",//E_USER_NOTICE
        2048=>  "Strict",//E_STRICT
        8192=>  "Deprecated",//E_DEPRECATED
    );


    // 6135 - local code, 30711 - server
	if (in_array($errLevel,  array(6135)))
        return;

	if (($errLevel & $errNo) == $errNo)
        return;


    $info = array(
        'errNo'  	 => $types[$errNo],
        'errMsg'	 => $errMsg,
        'backtrace'  => array(),
    );


    $files = debug_backtrace();
    for ($i=1, $n=sizeof($files); $i<$n; ++$i)
    	if ($files[$i]['function'] != "user_error")
	        $info['backtrace'][] = (isset($files[$i]['class'])? $files[$i]['class']."::":"").$files[$i]['function']."() ".', '.$files[$i]['file'].":".$files[$i]['line'];

	$info['backtrace'] = array_reverse($info['backtrace']);
	$info['backtrace'][] = ''.$fileName.":".$lineNum;
	$info['context'] = $vars;

    $view = new View('tpl/view_error');
    $tpl = $view->setVar("info",$info);
    $tpl = $view->setVar("log",_log()->get());
    echo $view->fetch();
    exit;
}

set_error_handler('_ehandler');
?>